package ru.vsu.cs.pm.pages;

import org.apache.wicket.feedback.ComponentFeedbackMessageFilter;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.*;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;
import ru.vsu.cs.pm.BasePage;
import ru.vsu.cs.pm.PMSession;
import ru.vsu.cs.pm.beans.AccountManager;
import ru.vsu.cs.pm.beans.DealerShipManager;
import ru.vsu.cs.pm.beans.ItemManager;
import ru.vsu.cs.pm.exceptions.IllegalAmountOfMoney;
import ru.vsu.cs.pm.models.Account;
import ru.vsu.cs.pm.models.DealerShip;
import ru.vsu.cs.pm.models.DealerShipModel;
import ru.vsu.cs.pm.models.Item;

import java.util.Date;
import java.util.List;

/**
 * User: ohodyakov
 * Date: 04.01.2011
 * Time: 12:33:39
 */
@MountPath(path = "transfer")
public class MoneyTransferPage extends BasePage {

    @SpringBean
    private DealerShipManager dealerShipManager;

    @SpringBean
    private AccountManager accountManager;

    @SpringBean
    private ItemManager itemManager;

    public MoneyTransferPage() {
        this(new DealerShipModel());
    }

    public MoneyTransferPage(DealerShip dealerShip) {
        this(new DealerShipModel(dealerShip));
    }

    public MoneyTransferPage(IModel<DealerShip> dealerShipModel) {
        boolean isNewMoneyTransfer = (dealerShipModel.getObject().getId() == null);

        add(new Label("moneyTransferMessage", isNewMoneyTransfer ?
                "Add new money transfer" : "Edit money transfer"));
        add(new MoneyTransferForm("moneyTransferForm", dealerShipModel));
    }

    private class MoneyTransferForm extends Form<DealerShip> {

        public MoneyTransferForm(String wicketId, IModel<DealerShip> dealerShipModel) {
            super(wicketId, new CompoundPropertyModel<DealerShip>(dealerShipModel));

            List<Account> accountList = getAccountList();
            DropDownChoice<Account> accountFromField = new DropDownChoice<Account>("accountFrom",
                    accountList, new IChoiceRenderer<Account>() {
                @Override
                public Object getDisplayValue(Account account) {
                    return account.getTitle();
                }
                @Override
                public String getIdValue(Account account, int index) {
                    return Integer.toString(index);
                }
            });
            add(accountFromField);

            DropDownChoice<Account> accountToField = new DropDownChoice<Account>("accountTo",
                    accountList, new IChoiceRenderer<Account>() {
                @Override
                public Object getDisplayValue(Account account) {
                    return account.getTitle();
                }
                @Override
                public String getIdValue(Account account, int index) {
                    return Integer.toString(index);
                }
            });
            add(accountToField);

            TextField<String> descriptionField = new TextField<String>("description");
            descriptionField.setRequired(false);
            add(descriptionField);

            TextField<Long> moneyField = new TextField<Long>("money");
            moneyField.setRequired(true);
            add(moneyField);

            add(new FeedbackPanel("moneyTransferFeedback", new ComponentFeedbackMessageFilter(this)));

            Button cancelBtn = new Button("cancelButton"){
                public void onSubmit() {
                    setRedirect(true);
                    setResponsePage(MoneyTransferListPage.class);
                }
            };

            cancelBtn.setDefaultFormProcessing(false);
            add(cancelBtn);
        }

        protected void onSubmit() {
            DealerShip moneyTransfer = getModelObject();

            try {
                dealerShipManager.transferMoney(moneyTransfer, PMSession.get().getAuthenticatedUser());
                setRedirect(true);
                setResponsePage(MoneyTransferListPage.class);
            } catch (IllegalAmountOfMoney illegalAmountOfMoney) {
                error("Not enough money to transfer");
            }
        }
    }

    private List<Account> getAccountList() {
        return accountManager.getAccountsForUser(PMSession.get().getAuthenticatedUser());
    }
}
